package com.ihr360.payroll.service.alps.profile;

import com.google.common.collect.ImmutableList;
import com.ihr360.alps.IServiceProfile;
import com.ihr360.alps.PropertyDescriptor;
import com.ihr360.alps.PropertyType;
import com.ihr360.alps.ServiceDescriptor;
import com.ihr360.alps.api.ServiceProvider;
import com.ihr360.commons.model.type.WorkHourType;
import com.ihr360.expr.parser.ExprToken;
import com.ihr360.payroll.service.alps.profile.provider.ProfileParamProvider;
import com.ihr360.payroll.service.alps.profile.provider.StandardOverTimeParamProvider;
import com.ihr360.payroll.service.alps.profile.provider.SyntheticalParamProvider;

import java.util.List;

/**
 * 加班工资规则Alps定义
 */
public class OverTimeRuleServiceProfile extends BaseServiceProfile {

    public static final class DEFAULT_VAL{
        public static final double NUMOP_RATIO=100.00;
        //综合工时默认平时加班比例
        public static final double S_NUMOP_RATIO=150.00;
        public static final double WEEKEND_RATIO=200.00;
        public static final double HOLIDAY_RATIO=300.00;
        public static final double FIXED_BASE=0.00;
        public static final double BASE_RATIO=100.00;
        public static final double MONTHLY_WORK_DAYS = 21.75;
        public static final double DAILY_WORK_HOURS = 8;
        public static final double MONTHLY_WORK_HOURS = 167.6;
    }

    //标准工时
    public static final OverTimeRuleServiceProfile numOpRatio = new OverTimeRuleServiceProfile(0, "平日比例", "numOpRatio",DEFAULT_VAL.NUMOP_RATIO);
    public static final OverTimeRuleServiceProfile weekendRatio = new OverTimeRuleServiceProfile(1, "周末比例", "weekendRatio",DEFAULT_VAL.WEEKEND_RATIO);
    public static final OverTimeRuleServiceProfile holidayRatio = new OverTimeRuleServiceProfile(2, "节假日比例", "holidayRatio",DEFAULT_VAL.WEEKEND_RATIO);
    public static final OverTimeRuleServiceProfile basementType = new OverTimeRuleServiceProfile(3, "基数类型", "basementType",0);

    public static final OverTimeRuleServiceProfile calcBase = new OverTimeRuleServiceProfile(5, "加班工资基数", "calcBase",DEFAULT_VAL.FIXED_BASE);

    //综合工时
    //多少小时每月
    public static final OverTimeRuleServiceProfile sNumOpRatio = new OverTimeRuleServiceProfile(0, "平日比例", "numOpRatio",DEFAULT_VAL.S_NUMOP_RATIO);
    public static final OverTimeRuleServiceProfile workCount = new OverTimeRuleServiceProfile(0, "周期工时周期标准", "workCount",DEFAULT_VAL.MONTHLY_WORK_HOURS);


    OverTimeRuleServiceProfile(int index, String name, String code,double defaultValue) {
        super(index,name,code,defaultValue);
    }


    public static ServiceDescriptor serviceDescriptor() {
        List<PropertyDescriptor> propertyDescriptors = ImmutableList
                .of(
                        //标准工时
                        PropertyDescriptor.builder()
                                .id(buildCode(WorkHourType.STANDARD, numOpRatio))
                                .name(buildName(WorkHourType.STANDARD, numOpRatio))
                                .type(PropertyType.String)
                                .build(),
                        PropertyDescriptor.builder()
                                .id(buildCode(WorkHourType.STANDARD, weekendRatio))
                                .name(buildName(WorkHourType.STANDARD, weekendRatio))
                                .type(PropertyType.String)
                                .build(),
                        PropertyDescriptor.builder()
                                .id(buildCode(WorkHourType.STANDARD, holidayRatio))
                                .name(buildName(WorkHourType.STANDARD, holidayRatio))
                                .type(PropertyType.String)
                                .build(),
                        PropertyDescriptor.builder()
                                .id(buildCode(WorkHourType.STANDARD, basementType))
                                .name(buildName(WorkHourType.STANDARD, basementType))
                                .type(PropertyType.String)
                                .build(),
                        PropertyDescriptor.builder()
                                .id(buildCode(WorkHourType.STANDARD, baseRatio))
                                .name(buildName(WorkHourType.STANDARD, baseRatio))
                                .type(PropertyType.String)
                                .build(),
                        PropertyDescriptor.builder()
                                .id(buildCode(WorkHourType.STANDARD, calcBase))
                                .name(buildName(WorkHourType.STANDARD, calcBase))
                                .type(PropertyType.String)
                                .build(),
                        PropertyDescriptor.builder()
                                .id(buildCode(WorkHourType.STANDARD, monthWorkCount))
                                .name(buildName(WorkHourType.STANDARD, monthWorkCount))
                                .type(PropertyType.String)
                                .build(),
                        PropertyDescriptor.builder()
                                .id(buildCode(WorkHourType.STANDARD, dayWorkCount))
                                .name(buildName(WorkHourType.STANDARD, dayWorkCount))
                                .type(PropertyType.String)
                                .build(),

                        //综合工时
                        PropertyDescriptor.builder()
                                .id(buildCode(WorkHourType.SYNTHETICAL, workCount))
                                .name(buildName(WorkHourType.SYNTHETICAL, workCount))
                                .type(PropertyType.String)
                                .build(),
                        PropertyDescriptor.builder()
                                .id(buildCode(WorkHourType.SYNTHETICAL, numOpRatio))
                                .name(buildName(WorkHourType.SYNTHETICAL, numOpRatio))
                                .type(PropertyType.String)
                                .build(),
                        PropertyDescriptor.builder()
                                .id(buildCode(WorkHourType.SYNTHETICAL, holidayRatio))
                                .name(buildName(WorkHourType.SYNTHETICAL, holidayRatio))
                                .type(PropertyType.String)
                                .build(),
                        PropertyDescriptor.builder()
                                .id(buildCode(WorkHourType.SYNTHETICAL, basementType))
                                .name(buildName(WorkHourType.SYNTHETICAL, basementType))
                                .type(PropertyType.String)
                                .build(),
                        PropertyDescriptor.builder()
                                .id(buildCode(WorkHourType.SYNTHETICAL, baseRatio))
                                .name(buildName(WorkHourType.SYNTHETICAL, baseRatio))
                                .type(PropertyType.String)
                                .build(),
                        PropertyDescriptor.builder()
                                .id(buildCode(WorkHourType.SYNTHETICAL, calcBase))
                                .name(buildName(WorkHourType.SYNTHETICAL, calcBase))
                                .type(PropertyType.String)
                                .build(),
                        PropertyDescriptor.builder()
                                .id(buildCode(WorkHourType.SYNTHETICAL, monthWorkCount))
                                .name(buildName(WorkHourType.SYNTHETICAL, monthWorkCount))
                                .type(PropertyType.String)
                                .build(),
                        PropertyDescriptor.builder()
                                .id(buildCode(WorkHourType.SYNTHETICAL, dayWorkCount))
                                .name(buildName(WorkHourType.SYNTHETICAL, dayWorkCount))
                                .type(PropertyType.String)
                                .build()
                );
        return ServiceDescriptor.builder()
                .serviceId(ServiceProvider.SERVICE_ID.SALARY_OT_RULE)
                .name("加班工资规则")
                .path("/alps/" + ServiceProvider.SERVICE_ID.SALARY_OT_RULE)
                .descriptors(propertyDescriptors)
                .build();
    }

//    public static List<Pair<String,Expr>> serviceVariables(OvertimeRuleWorkHourConfig config,OvertimeRule rule){
//        return ImmutableList.of(
//                Pair.of(buildVariableId(WorkHourType.STANDARD,numOpRatio),buildExpr(WorkHourType.STANDARD,numOpRatio,rule.getNumOpRatio()))
//        );
//    }

    public static ProfileParamProvider standardParamProvder = new StandardOverTimeParamProvider();

    public static ProfileParamProvider syntheticalParamProvder = new SyntheticalParamProvider();


    public static String buildVariableId(WorkHourType type, IServiceProfile profile){
        StringBuilder builder = new StringBuilder(ServiceProvider.SERVICE_ID.SALARY_OT_RULE);
        builder.append(ExprToken.NS_FIELD_CONNECTOR.val);
        builder.append(type.toString());
        builder.append(ServiceProvider.FIELD_CONNECTOR);
        builder.append(profile.getCode());
        return builder.toString();
    }



    private static String buildCode(WorkHourType type, IServiceProfile profile) {
        StringBuilder builder = new StringBuilder(type.toString());
        builder.append(ServiceProvider.FIELD_CONNECTOR);
        builder.append(profile.getCode());
        return builder.toString();
    }

    private static String buildName(WorkHourType type, IServiceProfile profile) {
        StringBuilder builder = new StringBuilder(type.getTitle());
        builder.append(profile.getName());
        return builder.toString();
    }
}